package com.mpj.servlet;

import com.mpj.util.HttpClientUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ForwardingServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ForwardingServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		HttpClientUtil.doGet("http://203.189.170.43/mpj2/jsp/sys/Sf_render.jsp?hf_s_id=WD11&hf_srv_id=Zv_jcus_logout", null, "utf-8");
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/json");
		PrintWriter out = response.getWriter();
		String link = request.getParameter("url") + "?";

		Map<String, String> params = new HashMap<>();
		Enumeration<String> enumeration = request.getParameterNames();
		int i = 0;
		while (enumeration.hasMoreElements()) {
			String key = enumeration.nextElement();
			if (key.equals("url")) {
				continue;
			}

//			params.put(key, request.getParameter(key));

			//link += key + "=" + URLEncoder.encode(request.getParameter(key), "utf-8");

			String [] parameters = request.getParameterValues(key);
			int len = parameters.length;
			if( len > 1){
				for ( int j = 0; j<len; j++){
					if (i > 0) {
						link += "&";
					}
					link += key + "=" + parameters[j];
				}
			}else {
				if (i > 0) {
					link += "&";
				}
				link += key + "=" + URLEncoder.encode(request.getParameter(key), "utf-8");
			}

			i ++;
		}

		String result = HttpClientUtil.doGet(link, null, "utf-8");
		out.print(result);
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json");
		PrintWriter out = response.getWriter();
		String link = request.getParameter("url");

		Map<String, String> params = new HashMap<>();
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String key = enumeration.nextElement();
			if (key.equals("url")) {
				continue;
			}
			params.put(key, request.getParameter(key));
		}

		String result = HttpClientUtil.doPost(URLEncoder.encode(link, "utf-8"), params, "utf-8");
		out.print(result);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		HttpClientUtil.setting(true);
	}

}
